package com.asm.AshtonUtil;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.util.Scanner;

@Slf4j
public class 发送JSON字符串3 {

    public static void main(String[] args) throws InterruptedException {

        final NioEventLoopGroup group = new NioEventLoopGroup();

        final Channel channel = new Bootstrap()
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
//                        ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));  //----- 日志 ------
                        ch.pipeline().addLast(new StringEncoder());
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//                                super.channelRead(ctx, msg);
                                ByteBuf buffer = (ByteBuf) msg;
                                log.debug("反馈消息（buffer）：" + buffer.toString());
                                log.debug("反馈消息（解码后 ）：" + buffer.toString(Charset.defaultCharset()));

                            }
                        });

                    }
                })
                .connect("47.100.173.102", 6611).sync().channel();
//                .connect("114.217.18.243", 6611).sync().channel();


        new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            while(true){

                final String line = scanner.nextLine();
                if ("q".equals(line))
                {
                    channel.close();
                    break;
                }else{
                    log.debug("发送消息：{}", line);

                }

                channel.writeAndFlush(line);
            }

        }).start();
/*  0010171427192300
         closeFuture() 异步等待 channel关闭才执行
         {"a":"1","b":"2","c":"3"}
         ### 可重复使用的 登录salt ###
         {"msgType":"login","appId":"977657829306ba43220f903ea6f6a7de","salt":"hb12345678","token":"9a2e98dedd149e6e25607e83a4e6fbd7"}
         ### 不可重复使用 ###
         {"msgType":"login","appId":"977657829306ba43220f903ea6f6a7de","salt":"hb123","token":"68e7287711f78216eeb4740caeb0a42e"}

         {"msgType":"register","collectorId":"1234567890123456","modeType":"4"}{"msgType":"re

        {"msgType":"productReporting","collectorId":"0010171427192300","processedQuantity":4,"token":"ytyguddzjynyhwsijakmzjoqxnukfiwb","appId":"977657829306ba43220f903ea6f6a7de" }

{"msgType":"stateChange","collectorId":"0010171331829600","status":"2","collectorStatus":"2","token":"ytyguddzjynyhwsijakmzjoqxnukfiwb","appId":"977657829306ba43220f903ea6f6a7de","input":"11100000","output":"00000011" }

*/
        channel.closeFuture().addListener(future -> {
            group.shutdownGracefully();
        });

    }


}
